//
//  OPNoteImageViewController.m
//  OnePiece
//
//  Created by lanou on 15/10/27.
//  Copyright © 2015年 wsy. All rights reserved.
//

#import "OPNoteImageViewController.h"
#import "OPNoteCollectionCell.h"
#import "UMSocial.h"


@interface OPNoteImageViewController () <UIScrollViewDelegate, UICollectionViewDataSource, UICollectionViewDelegate,UMSocialUIDelegate>
@property (nonatomic, strong) UIButton *backBtn;
@property (nonatomic, strong) UIProgressView *progress;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, assign) BOOL click;
@property (nonatomic, assign) BOOL zooming;

@end

@implementation OPNoteImageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT);
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    _collectionView = [[UICollectionView alloc]initWithFrame:[[UIScreen mainScreen]bounds] collectionViewLayout:layout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.pagingEnabled = YES;
    _collectionView.contentOffset = CGPointMake(SCREEN_WIDTH * _num, 0);
    [_collectionView registerClass:[OPNoteCollectionCell class] forCellWithReuseIdentifier:@"cell"];
    [self.view addSubview:_collectionView];
    
    _backBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    _backBtn.frame = CGRectMake(20, 40, 40, 30);
    _backBtn.backgroundColor = [UIColor colorWithWhite:0.000 alpha:0.502];
    _backBtn.layer.cornerRadius = 5;
    [_backBtn setImage:[UIImage imageNamed:@"zyciconfont-houtui"] forState:(UIControlStateNormal)];
    [_backBtn addTarget:self action:@selector(back) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:_backBtn];
    
    _progress = [[UIProgressView alloc]initWithProgressViewStyle:(UIProgressViewStyleDefault)];
    _progress.frame = CGRectMake(20, SCREEN_HEIGHT * 0.9 + 20, SCREEN_WIDTH - 40, 10);
    _progress.progressTintColor = [UIColor whiteColor];
    _progress.trackImage = [UIImage imageNamed:@"zyciconfont-anniu"];
    [self.view addSubview:_progress];
    
    
    
}


- (void)didClickLikeBtn:(UIButton *)btn
{
    btn.selected = !btn.selected;
    if (btn.selected) {
        [btn setImage:[UIImage imageNamed:@"zyciconfont-hongxin (3)"] forState:(UIControlStateNormal)];
    }
    else
    {
        [btn setImage:[UIImage imageNamed:@"zyciconfont-hongxin (2)"] forState:(UIControlStateNormal)];
    }
}

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"保存成功！" message:nil preferredStyle:(UIAlertControllerStyleAlert)];
    [self presentViewController:alert animated:YES completion:^{
        [alert dismissViewControllerAnimated:YES completion:nil];
    }];
}


- (void)didClickShareBtn
{
    OPNoteCollectionCell *cell = (OPNoteCollectionCell *)[self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:_collectionView.contentOffset.x / SCREEN_WIDTH inSection:0]];
    int i = _collectionView.contentOffset.x / SCREEN_WIDTH;
    NSDictionary *m = self.records[i];
    NSString *str = [((UILabel *)cell.words.subviews[0]).text substringWithRange:NSMakeRange(0, 40)];
    [UMSocialSnsService presentSnsIconSheetView:self
                                         appKey:@"562dcfa4e0f55a2a14004075"
                                      shareText:[NSString stringWithFormat:@"\"%@...\" 来自 %@的路上 by: OnePiece",str, m[@"owner"][@"nickname"] ]
                                     shareImage:cell.coverImg.image
                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToTencent,UMShareToRenren,UMShareToQQ,nil]
                                       delegate:self];
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _records.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    OPNoteCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    NSDictionary *dic = _records[indexPath.item];
    [cell configureDataWithDic:dic];
    [cell.like addTarget:self action:@selector(didClickLikeBtn:) forControlEvents:(UIControlEventTouchUpInside)];
    [cell.share addTarget:self action:@selector(didClickShareBtn) forControlEvents:(UIControlEventTouchUpInside)];
    cell.page.text = [NSString stringWithFormat:@"%ld / %ld", indexPath.row, _records.count];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(didClickWordsLabel:)];
    [cell.words addGestureRecognizer:tap];
    UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(zoomout:)];
    [tap2 setNumberOfTapsRequired:2];
    [cell.scrollView addGestureRecognizer:tap2];
    return cell;
}

- (void)zoomout:(UITapGestureRecognizer *)tap
{
    _zooming = !_zooming;
    if (_zooming) {
        [UIView animateWithDuration:0.5 animations:^{
            UIImageView *a = tap.view.subviews[0];
            a.bounds = CGRectMake(0, 0, a.bounds.size.width * 2, a.bounds.size.height * 2);
            ((UIScrollView *)tap.view).contentSize = CGSizeMake(a.bounds.size.width * 2, a.bounds.size.height);
        }];
    }
    else
    {
        [UIView animateWithDuration:0.5 animations:^{
            UIImageView *a = tap.view.subviews[0];
            a.bounds = CGRectMake(0, 0, SCREEN_WIDTH, a.bounds.size.height * SCREEN_WIDTH / a.bounds.size.width);
            ((UIScrollView *)tap.view).contentSize = CGSizeMake(SCREEN_WIDTH, a.bounds.size.height * SCREEN_WIDTH / a.bounds.size.width);
        }];
    }

}

- (void)didClickWordsLabel:(UITapGestureRecognizer *)tap
{
    UILabel *label = tap.view.subviews[0];
    CGFloat labH = label.frame.size.height;
    
    int i = self.collectionView.contentOffset.x / SCREEN_WIDTH;
    NSDictionary *dic = self.records[i];
    if (((NSString *)dic[@"picfile"]).length > 0) {
        _click = !_click;
        if (labH > SCREEN_HEIGHT * 0.2 - 10 && labH < SCREEN_HEIGHT * 0.9 - 70) {
            if (_click) {
                [UIView animateWithDuration:0.5 animations:^{
                    tap.view.frame = CGRectMake(0, SCREEN_HEIGHT * 0.9 - 10 - labH, SCREEN_WIDTH, labH);
                }];
            }
            else
            {
                [UIView animateWithDuration:0.5 animations:^{
                    tap.view.frame = CGRectMake(0,  SCREEN_HEIGHT * 0.7 , SCREEN_WIDTH, SCREEN_HEIGHT * 0.2 - 10);
                }];
            }
            
        }
        else if(labH > SCREEN_HEIGHT * 0.9 - 70)
        {
            
            if (_click) {
                [UIView animateWithDuration:0.5 animations:^{
                    tap.view.frame = CGRectMake(0, 60, SCREEN_WIDTH, SCREEN_HEIGHT * 0.9 - 70);
                }];

            }
            else
            {
                [UIView animateWithDuration:0.5 animations:^{
                    tap.view.frame = CGRectMake(0,  SCREEN_HEIGHT * 0.7 , SCREEN_WIDTH, SCREEN_HEIGHT * 0.2 - 10);
                }];
                
            }
        }
    }
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"----");
}


- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    self.progress.progress = scrollView.contentOffset.x / scrollView.contentSize.width;
}



- (void)back
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
